<template>
    <div id="canvas" style="width: 100%;height: 100%;" ref="canvas"></div>
</template>
  
<script setup lang="ts">
import { ref, onMounted } from 'vue';
import * as echarts from 'echarts'
const canvas = ref() // dom实例
let myChart: any = null // echarts实例

onMounted(() => {
    setTimeout(() => {
        renderChart()
    }, 200);

})
const renderChart = () => {
    myChart = echarts.init(canvas.value)

    let xaxisData = [
        '急救箱',
        '止血带',
        '呼吸面罩',
        '急救手册',
        '缝合针线包',
        '应急药物',
        '心肺复苏模型',
        '冰袋',
        '救护担架']
    let yaxisData = [400, 280, 120, 200, 330, 250, 237, 301, 134]
    const offsetX = 10;
    const offsetY = 5;
    // 绘制左侧面
    const CubeLeft = echarts.graphic.extendShape({
        shape: {
            x: 0,
            y: 0,
        },
        buildPath: function (ctx, shape) {
            // 会canvas的应该都能看得懂，shape是从custom传入的
            const xAxisPoint = shape.xAxisPoint;
            const c0 = [shape.x, shape.y];
            const c1 = [shape.x - offsetX, shape.y - offsetY];
            const c2 = [xAxisPoint[0] - offsetX, xAxisPoint[1] - offsetY];
            const c3 = [xAxisPoint[0], xAxisPoint[1]];
            ctx
                .moveTo(c0[0], c0[1])
                .lineTo(c1[0], c1[1])
                .lineTo(c2[0], c2[1])
                .lineTo(c3[0], c3[1])
                .closePath();
        },
    });

    // 绘制右侧面
    const CubeRight = echarts.graphic.extendShape({
        shape: {
            x: 0,
            y: 0,
        },
        buildPath: function (ctx, shape) {
            const xAxisPoint = shape.xAxisPoint;
            const c1 = [shape.x, shape.y];
            const c2 = [xAxisPoint[0], xAxisPoint[1]];
            const c3 = [xAxisPoint[0] + offsetX, xAxisPoint[1] - offsetY];
            const c4 = [shape.x + offsetX, shape.y - offsetY];
            ctx
                .moveTo(c1[0], c1[1])
                .lineTo(c2[0], c2[1])
                .lineTo(c3[0], c3[1])
                .lineTo(c4[0], c4[1])
                .closePath();
        },
    });

    // 绘制顶面
    const CubeTop = echarts.graphic.extendShape({
        shape: {
            x: 0,
            y: 0,
        },
        buildPath: function (ctx, shape) {
            const c1 = [shape.x, shape.y];
            const c2 = [shape.x + offsetX, shape.y - offsetY]; //右点
            const c3 = [shape.x, shape.y - offsetX];
            const c4 = [shape.x - offsetX, shape.y - offsetY];
            ctx
                .moveTo(c1[0], c1[1])
                .lineTo(c2[0], c2[1])
                .lineTo(c3[0], c3[1])
                .lineTo(c4[0], c4[1])
                .closePath();
        },
    });

    // 注册三个面图形
    echarts.graphic.registerShape("CubeLeft", CubeLeft);
    echarts.graphic.registerShape("CubeRight", CubeRight);
    echarts.graphic.registerShape("CubeTop", CubeTop);

    let option = {
        tooltip: {
            trigger: "axis",
            axisPointer: {
                type: "shadow",
            },
            backgroundColor: "rgba(255,255,255,0.75)",
            extraCssText: "box-shadow: 2px 2px 4px 0px rgba(0,0,0,0.3);",
            textStyle: {
                fontSize: 14,
                color: "#000",
            },
            formatter: (params, ticket, callback) => {
                const item = params[1];
                return item.name + " : " + item.value + " 个";
            },
        },
        grid: {
            left: "1%",
            right: "4%",
            top: "20%",
            bottom: "0%",
            containLabel: true,
        },
        xAxis: {
            type: 'category',
            data: xaxisData,
            axisLine: {
                show: true,
                lineStyle: {
                    width: 1,
                    color: '#008de7'
                }
            },
            axisTick: {
                show: false
            },
            axisLabel: {
                fontSize: 14,
                color: '#FFFFFF'
            }
        },
        yAxis: {
            name: "数量",
            type: 'value',
            nameTextStyle: {
                color: '#fff',
                fontWeight: 400,
                fontSize: 14
            },
            axisLine: {
                show: true,
                lineStyle: {
                    width: 1,
                    color: '#008de7'
                }
            },
            splitLine: {
                show: true,
                lineStyle: {
                    color: '#008de7',
                    type: 'dashed'
                }
            },
            axisTick: {
                show: false
            },
            axisLabel: {
                fontSize: 14,
                color: '#FFFFFF'
            }
        },
        series: [
            {
                type: "custom",
                renderItem: (params, api) => {
                    const location = api.coord([api.value(0), api.value(1)]);
                    return {
                        type: "group",
                        children: [
                            {
                                type: "CubeLeft",
                                shape: {
                                    api,
                                    xValue: api.value(0),
                                    yValue: api.value(1),
                                    x: location[0],
                                    y: location[1],
                                    xAxisPoint: api.coord([api.value(0), 0]),
                                },
                                style: {
                                    fill: new echarts.graphic.LinearGradient(0, 1, 0, 0, [
                                        {
                                            offset: 0,
                                            color: '#09f7f9',
                                        },
                                        {
                                            offset: 1,
                                            color: '#09f7f920'
                                        }
                                    ]),
                                },
                            },
                            {
                                type: "CubeRight",
                                shape: {
                                    api,
                                    xValue: api.value(0),
                                    yValue: api.value(1),
                                    x: location[0],
                                    y: location[1],
                                    xAxisPoint: api.coord([api.value(0), 0]),
                                },
                                style: {
                                    fill: new echarts.graphic.LinearGradient(0, 1, 0, 0, [
                                        {
                                            offset: 0,
                                            color: '#00a6a7',
                                        },
                                        {
                                            offset: 1,
                                            color: '#00a6a720'
                                        },
                                    ]),
                                },
                            },
                            {
                                type: "CubeTop",
                                shape: {
                                    api,
                                    xValue: api.value(0),
                                    yValue: api.value(1),
                                    x: location[0],
                                    y: location[1],
                                    xAxisPoint: api.coord([api.value(0), 0]),
                                },
                                style: {
                                    fill: '#09f7f8'
                                },
                            },
                        ],
                    };
                },
                data: yaxisData,
            },
            {
                type: "bar",
                itemStyle: {
                    color: "transparent",
                },
                data: yaxisData,
            },
        ],
    };

    myChart.setOption(option)
}
window.addEventListener("resize", function () {
    myChart?.resize();
});
</script>
  
<style lang="scss" scoped></style>
  